package org.hin.programmering1.engine;

import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * Sprite class for displaying images on the screen
 * 
 * @author Meanz
 */
public class Sprite extends GameObject implements Renderable {

	private Image spriteImage;
	
	/**
	 * Loads a sprite from the given image
	 * @param imageName
	 * @return
	 */
	public static Sprite loadSprite(String imageName) {
		Sprite sprite = null;
		try {
			sprite = new Sprite(ImageIO.read(new File(imageName)));
		} catch(IOException iex) {
			iex.printStackTrace();
		}
		
		return sprite;
		
	}
	
	/**
	 * Constructor
	 */
	public Sprite(Image image) {
		this.spriteImage= image;
	}
	
	/**
	 * Set the image that is supposed to be rendered
	 * @param image
	 * The new image to be rendered
	 */
	public void setImage(Image image) {
		this.spriteImage = image;
	}
	
	/**
	 * Get the image of this sprite
	 * @return
	 * The image of this sprite
	 */
	public Image getImage() {
		return spriteImage;
	}

	/**
	 * Render loop
	 */
	@Override
	public void render(Graphics g) {
		if(g == null) {
			return;
		}
		if(spriteImage != null) {
		
			g.drawImage(spriteImage, transform.position.x, transform.position.y, null);
		}
	}
	
}
